﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MTodo.ViewModel;
using WenslyWuLib.Common;
using WenslyWuLib.Common.DynamicEffect;
using MTodo.Model;
using MTodo.ViewInterface;

namespace MTodo.Pages
{
    public partial class MainPage : PageBase, IMainPage
    {
        #region Fields
        private TimeGroupsViewModel _groupsViewModel;
        private ListBoxItem _lastSelectedFolderItem = null;
        private NewMemo newWindow;
        #endregion

        #region Methods
        // Constructor
        public MainPage():base()
        {
            //InitPageView();
            //InitControls();

            newWindow = new NewMemo();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            _groupsViewModel = new TimeGroupsViewModel();

            this.TimeGroups.ItemsSource = _groupsViewModel.TimeGroups;
            this.TimeGroups.SelectionChanged += new SelectionChangedEventHandler(TimeGroups_SelectionChanged);
            this.TimeGroups.Tap += new EventHandler<GestureEventArgs>(TimeGroups_Tap);
            this.TimeGroups.Loaded += new RoutedEventHandler(TimeGroups_Loaded);

        
        }

        /// <summary>
        /// TimeGroup Loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void TimeGroups_Loaded(object sender, RoutedEventArgs e)
        {           
            for (int i = 0; i < 3; i++)
            {
                ListBoxItem item = this.TimeGroups.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
                item.Visibility = Visibility.Visible;
                List<ListBox> stack = Common.FindAllChildOfType<ListBox>(item);
                ListBox itemDtl = stack.FirstOrDefault();
               
                this.Dispatcher.BeginInvoke(() =>
                {
                    itemDtl.Visibility = Visibility.Visible;
                    itemDtl.Visibility = Visibility.Collapsed;
                });
            }
        }

        void TimeGroups_Tap(object sender, GestureEventArgs e)
        {
            var ctlSource = e.OriginalSource as Image;
            if (ctlSource != null)
            {
                GroupItem tag = ctlSource.Tag as GroupItem;
                // MessageBox.Show(tag.GroupName);
                addViewPop.Child = newWindow;
                addViewPop.IsOpen = true;
                newWindow.ShowDiaglog();
                e.Handled = true;
            }
        }

        void TimeGroups_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            #region
            ListBoxItem item = this.TimeGroups.ItemContainerGenerator.ContainerFromIndex((sender as ListBox).SelectedIndex) as ListBoxItem;
            item.Visibility = Visibility.Visible;
            List<ListBox> stack = Common.FindAllChildOfType<ListBox>(item);
            ListBox itemDtl = stack.FirstOrDefault();

            if (_lastSelectedFolderItem != null)
            {
                List<ListBox> lastStack = Common.FindAllChildOfType<ListBox>(_lastSelectedFolderItem);
                var ls = lastStack.FirstOrDefault();
                this.Dispatcher.BeginInvoke(() =>
                {
                    AnimationProvider.FadeOutObject(ls, CollapseItem,itemDtl);                 
                });
            }

            if (itemDtl != null && _lastSelectedFolderItem == null)
            {
                itemDtl.Visibility = Visibility.Visible;
                this.Dispatcher.BeginInvoke(() =>
                {
                    AnimationProvider.RightToLeftAnimation(itemDtl, this);
                });
            }
            _lastSelectedFolderItem = item;

            #endregion
        }

        private void CollapseItem(Object item)
        {
            ListBox itemDtl = item as ListBox;
            if (itemDtl == null)
                return;

            if (itemDtl != null)
            { itemDtl.Visibility = Visibility.Visible; }
            this.Dispatcher.BeginInvoke(() =>
            {
                AnimationProvider.RightToLeftAnimation(itemDtl, this);
            });
        }
        #endregion

        #region SystemMethods

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (addViewPop.IsOpen)
            {
                newWindow.HiddenDialog(() => { addViewPop.IsOpen = false; });
                e.Cancel = true;
            }
        }

        #endregion
    }
}